package com.todod.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.Dictdto;
import com.todod.entity.QueryEntry;
import com.todod.exception.CustomException;
import com.todod.mapper.DictMapper;
import com.todod.model.Dict;
import com.todod.service.IDictService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gsh
 * @since 2020-12-04
 */
@Service
@Transactional
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

	@Autowired
	private DictMapper dictMapper;

	/**
	 * @title 根据业务字典类型获取字典项
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getDictListByCode(String code) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", code);
		queryWrapper.eq("sd_flag", "2");
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	@Override
	public String getMaxCodeByCode(String code) {
		return dictMapper.getMaxCodeByCode(code);
	}

	/**
	 * @title 根据业务字典类型获取启用状态的字典项
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getDictListByCode2(String code) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", code);
		queryWrapper.eq("sd_flag", "2");
		queryWrapper.eq("sd_state", "1");
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	/**
	 * @title 根据业务字典类型获取字典项
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getSysDictListByCode(String code) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_code", code);
		queryWrapper.eq("sd_flag", "2");
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	/**
	 * @title 根据业务字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getList(QueryEntry queryEntry) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		if (null != queryEntry) {
			if (null != queryEntry.getName() && !queryEntry.getName().equals("")) {
				queryWrapper.like("sd_name", queryEntry.getName());
			}
			if (null != queryEntry.getCode() && !queryEntry.getCode().equals("")) {
				queryWrapper.like("sd_code", queryEntry.getCode());
			}
			if (null != queryEntry.getBeginDate() && !queryEntry.getBeginDate().equals("")) {
				queryWrapper.gt("sd_create_time", queryEntry.getBeginDate());
			}
			if (null != queryEntry.getEndDate() && !queryEntry.getEndDate().equals("")) {
				queryWrapper.lt("sd_create_time", queryEntry.getEndDate() + " 23:59:59");
			}
			if (null != queryEntry.getState() && !queryEntry.getState().equals("")) {
				queryWrapper.like("sd_state", queryEntry.getState());
			}
		}
		queryWrapper.orderByDesc("sd_create_time");
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	/**
	 * @title 根据顶级字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getParentList(String code) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_id", "0");
		if (null != code && !"".equals(code)) {
			queryWrapper.eq("sd_code", code);
		}
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	/**
	 * @title 根据顶级字典列表
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public List<Dict> getListByParentCode(String parentCode) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		if (null != parentCode && !"".equals(parentCode)) {
			queryWrapper.eq("sd_parent_code", parentCode);
		}
		List<Dict> dictList = list(queryWrapper);
		return dictList;
	}

	/**
	 * @title 根据父字典代码和字典代码获取字典
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	public Dict getDictByParentCodeAndCode(Long pId, String code) {

		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_id", pId);
		queryWrapper.eq("sd_code", code);
		queryWrapper.eq("sd_flag", "2");
		Dict selectOne = getOne(queryWrapper);
		return selectOne;
	}

	/**
	 * @title 新增数据
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	@CacheEvict(value = "op:dict", allEntries = true) // 方法执行后清空所有dict空间缓存
	public void saveData(Long userId, Dict dict, Long pId) {

		dict.setId(SnowflakeIdGenerator.getId());
		if (null != pId) {
			// 检擦是否有重复的次级code
			Dict dictByParentCodeAndCode = getDictByParentCodeAndCode(pId, dict.getCode());
			if (null != dictByParentCodeAndCode) {
				throw new CustomException("字典代码重复！请重新输入");
			}
			dict.setParentId(pId);
			dict.setFlag("2");
			Dict selectById = getById(pId);
			int sortNum = getSortNum(pId, selectById.getCode());// 获取排序号
			dict.setSortNum(sortNum + 1);
			dict.setParentCode(selectById.getCode());

		} else {
			// 检擦是否有重复的顶级code
			List<Dict> parentList = getParentList(dict.getCode());
			if (parentList.size() > 0) {
				throw new CustomException("字典代码重复！请重新输入");
			}
			dict.setParentId(0l);
			dict.setFlag("1");
		}
		dict.setState("1");
		dict.setCreateUserId(userId);
		dict.setCreateTime(new Date());
		save(dict);
	}

	/**
	 * @title 编辑数据
	 * @author MengHaoPeng
	 * @date 2019-10-11
	 * @return List<Dict>
	 */
	@Override
	@CacheEvict(value = "op:dict", allEntries = true) // 方法执行后清空所有dict空间缓存
	public void editData(Long userId, Dict dict) {

		dict.setModifyTime(new Date());
		dict.setModifyUserId(userId);
		updateById(dict);
	}

	/**
	 * @title 删除数据
	 * @author MengHaoPeng
	 * @date 2019-10-11 * @return List<Dict>
	 */
	@Override
	@CacheEvict(value = "op:dict", allEntries = true) // 方法执行后清空所有dict空间缓存
	public void deleteData(Dict dict) {
		// 判断是否是顶级字典
		if (dict.getParentId() == 0l) {
			QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("sd_parent_id", dict.getId());
			remove(queryWrapper);
		}
		removeById(dict.getId());
	}

	/**
	 * @title 获取排序号
	 * @author MengHaoPeng
	 * @date 2019-10-11 * @return List<Dict>
	 */
	private int getSortNum(Long pId, String pCode) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_id", pId);
		queryWrapper.eq("sd_parent_code", pCode);
		List<Dict> list = list(queryWrapper);
		return list.size();
	}

	/**
	 * @title 根据父code和code获取dict
	 * @author MengHaoPeng
	 * @date 2019-10-11 * @return List<Dict>
	 */
	@Override
	public Dict getDictByPcodeAndCode(String pcode, String code) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", pcode);
		queryWrapper.eq("sd_code", code);
		queryWrapper.eq("sd_flag", "2");
		queryWrapper.eq("sd_state", "1");
		return getOne(queryWrapper);
	}

	/**
	 * 修改字典状态
	 * 
	 * @Title: updateState
	 * @Description:
	 * @author gsh
	 * @date 2020年4月12日
	 * @param userId
	 * @param id
	 * @param state
	 */
	@Override
	@CacheEvict(value = "op:dict", allEntries = true) // 方法执行后清空所有dict空间缓存
	public void updateState(Long userId, Long id, String state) {
		Dict dict = getById(id);
		if ("2".equals(dict.getFlag())) {
			dict.setState(state);
			dict.setModifyUserId(userId);
			dict.setModifyTime(new Date());

			updateById(dict);
		} else {
			throw new CustomException("父字典不支持此操作");
		}

	}

	@Override
	public List<Dictdto> getbyparendcode(String string) {
		DictMapper baseMapper2 = this.getBaseMapper();
		return baseMapper2.getbyparendcode(string);

	}

	/**
	 * @title 根据父code和code获取dict
	 * @author MengHaoPeng
	 * @date 2019-10-11 * @return List<Dict>
	 */
	public List<Dict> getDictEntryByDictTypeIdDictId(String pcode, List<String> code) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", pcode);
		queryWrapper.in("sd_code", code);
		queryWrapper.eq("sd_flag", "2");
		List<Dict> dictList = dictMapper.selectList(queryWrapper);
		return dictList;
	}

	/**
	 * 
	 * @param typeList
	 * @return
	 */
	public List<Dict> getDictByParentCodeBesidesCode(String parentCode, List<String> typeList) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", parentCode);
		queryWrapper.notIn("sd_code", typeList);
		queryWrapper.eq("sd_flag", "2");
		List<Dict> dictList = dictMapper.selectList(queryWrapper);
		return dictList;
	}

	@Override
	public Dict getDictByPcodeAndName(String pcode, String name) {
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sd_parent_code", pcode);
		queryWrapper.eq("sd_name", name);
		queryWrapper.eq("sd_flag", "2");
		queryWrapper.eq("sd_state", "1");
		return getOne(queryWrapper);
	}

}
